{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37764bit5d211478a4cf4a3d93a2c891f6a9207d",
   "display_name": "Python 3.7.7 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "'''\n",
    "列表推导式提供了一个更简单的创建列表的方法。常见的用法是把某种操作应用于序列或可迭代对象的每个元素上，然后使用其结果来创建列表，或者通过满足某些特定条件元素来创建子序列。\n",
    "'''\n",
    "\n",
    "squares = []\n",
    "for x in range(10):\n",
    "    squares.append(x**2)\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "squares = list(map(lambda x: x**2, range(10)))\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "squares = [x**2 for x in range(10)]\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "[(x,y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y] # for 和 if 的顺序是相同的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[-8, -4, 0, 4, 8]"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "vec = [-4, -2, 0, 2, 4]\n",
    "[x*2 for x in vec]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 2, 4]"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "[x for x in vec if x >= 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[4, 2, 0, 2, 4]"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "[abs(x) for x in vec]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[&#39;banaa&#39;, &#39;djfjsj&#39;]"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "freshfruit = ['   banaa','djfjsj   ']\n",
    "[fruit.strip() for fruit in freshfruit]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "[(x, x**2) for x in range(6)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "SyntaxError",
     "evalue": "invalid syntax (&lt;ipython-input-12-088e2c5c73bb&gt;, line 1)",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m&quot;&lt;ipython-input-12-088e2c5c73bb&gt;&quot;\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    [x, x**2 for x in range(6) for x in range(6)]\u001b[0m\n\u001b[0m             ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "[x, x**2 for x in range(6) for x in range(6)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "vec = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "[num for elem in vec for num in elem]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[&#39;3.1&#39;, &#39;3.14&#39;, &#39;3.142&#39;, &#39;3.1416&#39;, &#39;3.14159&#39;]"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "from math import pi\n",
    "[str(round(pi, i)) for i in range(1,6)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}